package cn.javadog.cloud.starter.jsonfilter.config;

import cn.javadog.cloud.starter.jsonfilter.converter.JsonFilterHttpMessageConverter;
import cn.javadog.cloud.starter.jsonfilter.converter.JsonFilterResponseBodyAdvice;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author yuy
 * @date 2020-11-09 15:41
 **/
@Configuration
@EnableConfigurationProperties(JsonFilterProperties.class)
@ConditionalOnProperty(prefix = "starter.jsonfilter", value = "enabled", havingValue = "true")
public class JsonFilterAutoConfiguration {

    @Bean("jsonFilterHttpMessageConverter")
    public HttpMessageConverters jsonFilterHttpMessageConverter() {
        Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        JsonFilterHttpMessageConverter jsonFilterHttpMessageConverter = new JsonFilterHttpMessageConverter();
        messageConverters.add(jsonFilterHttpMessageConverter);
        return new HttpMessageConverters(true, messageConverters);
    }

    @Bean("jsonFilterResponseBodyAdvice")
    public JsonFilterResponseBodyAdvice jsonFilterResponseBodyAdvice() {
        return new JsonFilterResponseBodyAdvice();
    }

}
